package com.csxy.xxm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csxy.xxm.JwtUtils;
import com.csxy.xxm.R;
import com.csxy.xxm.entity.TOrder;
import com.csxy.xxm.exception.MyException;
import com.csxy.xxm.service.OrderService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/edeorder/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

//    @GetMapping
//    public String list(){
//        return "asafs";
//    }

    //生成订单
    @PostMapping("/createOrder/{courseId}")
    public R createOrder(@PathVariable String courseId, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登录
        if (StringUtils.isEmpty(memberId)){
            throw new MyException(20001,"请登录");
        }
        //调用生成订单方法
        String orderNo = orderService.createOrders(courseId, memberId);
        return R.ok().data("orderId",orderNo);
    }

    //根据订单id查询订单信息
    @GetMapping("/getOrderInfo/{orderNo}")
    public R getOrderInfoById(@PathVariable("orderNo") String orderNo){
        //根据订单id查询订单信息
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderNo);
        TOrder tOrder = orderService.getOne(wrapper);
        return R.ok().data("item",tOrder);
    }
}
